//
//  StatusCell.swift
//  Tanenbaum_blog
//
//  Created by Tanenbaum on 15/8/2.
//  Copyright © 2015年 GZJ. All rights reserved.
//

import UIKit

let satusCellControlMargin: CGFloat = 8.0
enum StatusCellIdentifier: String {
    
    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    static func cellID(status:Status) -> String {
        
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}
class StatusCell: UITableViewCell {

    var status: Status? {
        didSet {
            
            topView.status = status
            contentLabel.text = status?.text ?? ""
            
            
            //设置配图的数据
            pictureView.status = status
            print(pictureView.bounds.size)
            
            pictureWidthCons?.constant = pictureView.bounds.size.width
            pictureHeightCons?.constant = pictureView.bounds.size.height
            pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : satusCellControlMargin
        }
    }
    
    //图片视图的宽度约束和高度约束
    var pictureWidthCons: NSLayoutConstraint?
    var pictureHeightCons: NSLayoutConstraint?
    var pictureTopCons: NSLayoutConstraint?
   
    func rowHeight(status:Status) ->CGFloat {
        
        self.status = status
        layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }

   required init?(coder aDecoder: NSCoder) {
      super.init(coder: aDecoder)
      setupUI()
    
   }
    
     func setupUI() {
        
        //添加视图
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        //添加约束
        //1 顶部视图
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 54))
        //2 内容视图
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: satusCellControlMargin, y: satusCellControlMargin))
        //宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -2 * satusCellControlMargin))
     
         // 4.底部视图
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.size.width, height: 44), offset: CGPoint(x: -satusCellControlMargin, y: satusCellControlMargin))

    }
    
    
    //MARK --懒加载
    //顶部视图
    private lazy var topView: StatusTopView = StatusTopView()
    //内容标签
     lazy var contentLabel: UILabel = {
       
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
        label.numberOfLines = 0
        label.preferredMaxLayoutWidth  = UIScreen.mainScreen().bounds.width - 2 * satusCellControlMargin
        return label
        
    }()
    //图片视图
     lazy var pictureView: StatusPictureView = StatusPictureView()
    
    //底部视图
     lazy var bottomView: StatusBottomView = StatusBottomView()
}
